package com.kedacom.demo.springrabbitex;

import javax.annotation.Resource;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Component;

import com.kedacom.demo.springrabbitex.RabbitMessageSender.PendingConfirm;

/**
 * Rabbit Publisher Confirm(Ack) Callback
 * @author luocanfeng
 * @date 2013-8-20
 */
@Component("confirmCallback")
public class ConfirmCallback implements RabbitTemplate.ConfirmCallback {

	@Resource
	private PendingConfirmHolder confirmHolder;

	public void setConfirmHolder(PendingConfirmHolder confirmHolder) {
		this.confirmHolder = confirmHolder;
	}

	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		if (correlationData == null) {
			return;
		}

		String msgTag = correlationData.getId();
		PendingConfirm pendingConfirm = confirmHolder.get(msgTag);

		// put the ack into the LinkedBlockingQueue of the pending confirm.
		pendingConfirm.getQueue().add(ack);
	}

}
